home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 579 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Semantics of "new foo[0]"
  5. Date: 28 Feb 1996 16:16:48 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4h1up3$l69@engnews1.Eng.Sun.COM>
  9. References: <MGB.96Feb27175316@kronecker.mitre.org>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: taumet.eng.sun.com
  14. Content-Length: 1427
  15. X-Lines: 41
  16. Originator: clamage@taumet
  17.  
  18. In article 96Feb27175316@kronecker.mitre.org, mgb@kronecker.mitre.org (G. Mike Butler D054) writes:
  19. >According to the ARM section 5.3.3:
  20. >     "operator new() can be called with the argument
  21. >      zero.  Repeated such calls return pointers to
  22. >      distinct objects."  
  23.      
  24. >But when I execute the following:
  25. >    #include <isotream.h>
  26. >    main()
  27. >    {
  28. >      struct foo { char a[1024]; };
  29. >      foo *p = new foo[0];
  30. >      foo *q = new foo[0];
  31. >
  32. >      cout << (void *)p << " " << (void *)q << endl;
  33. >    }
  34.  
  35. >I get
  36. >    0x338d8 0x338e8
  37.  
  38. >While these are distinct pointers, the pointers refer to overlapping
  39. >objects.
  40.  
  41. No, they don't. You asked for an array of zero elements. For an array A
  42. of n elements, you may access any elements from A[0] through A[n-1].
  43. In addition, the address of A[n] is a valid address for comparisons,
  44. but you may not access any object at that address, because there isn't
  45. one at that address. Substitute 0 for n, and you find that the
  46. expression A[i] is invalid for all values of 'i', but &A[0] is a valid
  47. address expression, as long as you don't try to access the (non-existant)
  48. object at that address.
  49.  
  50. Therefore, as long as the values of p and q are different, all
  51. requirements have been satisfied. You can't usefully describe them as
  52. pointing to "overlapping objects". There are no objects at all, and
  53. you can't dereference the pointers.
  54.  
  55. ---
  56. Steve Clamage, stephen.clamage@eng.sun.com
  57.  
  58.  
  59.  
  60.  
  61. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
  62.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  63.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  64.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  65.   Comments? mailto:std-c++-request@ncar.ucar.edu
  66. ]
  67.